package Niuke;

import jdk.management.resource.internal.inst.FileChannelImplRMHooks;
import sun.nio.ch.DirectBuffer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.HashMap;
import java.util.Stack;


//米哈呦8.7笔试
//最长无重复子串 和 单调栈
public class mihayou {
    public int lengthOfLongestSubstring (String s) {
        // write code here
        char[] chars = s.toCharArray();
        int maxlen = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int index = 0;
        int curlen = 0;
        while (index < chars.length) {
            if (map.containsKey(chars[index])) {
                int lastidx = map.get(chars[index]);
                if (index - lastidx <= curlen) {
                    curlen = index - lastidx;
                } else {
                    curlen++;
                }
            }else {
                curlen++;
            }
            maxlen = Math.max(maxlen, curlen);
            map.put(chars[index], index);
            index++;
        }
        return maxlen;
    }

    public int[] temperatures (int[] dailyTemperatures) {
        // write code here
        int[] res=new int[dailyTemperatures.length];
        res[res.length-1]=0;
        Stack<Integer> stack=new Stack<>();
        stack.push(res.length-1);
        for (int idx=res.length-2;idx>=0;idx--){
            if (dailyTemperatures[idx]<dailyTemperatures[stack.peek()]){
                res[idx]=stack.peek()-idx;
            }else {
                while (!stack.isEmpty()&&dailyTemperatures[stack.peek()]<=dailyTemperatures[idx])
                    stack.pop();
                if (!stack.isEmpty()){
                    res[idx]=stack.peek()-idx;
                }else
                    res[idx]=0;
            }
            stack.push(idx);
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        /*int res=new mihayou().lengthOfLongestSubstring("abcabcbb");
        System.out.println(res);
        int[] resarr=new mihayou().temperatures(new int[]{0,0,0,0,0,0});
        for (int i:resarr)
            System.out.print(i+"  ");*/

    }
}
